package com.sunday.boot3.user.center.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sunday.common.core.asserts.BusinessAssert;
import com.sunday.common.core.converter.MapperConvert;
import com.sunday.common.core.enums.ErrorCodeEnum;
import com.sunday.common.core.remoting.base.page.PageResultDTO;
import com.sunday.common.mybatis.plus.query.PageWrapper;
import com.sunday.common.leaf.core.IdWorker;
import com.sunday.boot3.user.center.pojo.dto.role.PageRoleResDTO;
import com.sunday.boot3.user.center.pojo.dto.role.SaveRoleReqDTO;
import com.sunday.boot3.user.center.pojo.dto.role.RemoveRoleReqDTO;
import com.sunday.boot3.user.center.pojo.dto.role.UpdateRoleReqDTO;
import com.sunday.boot3.user.center.pojo.dto.role.PageRoleReqDTO;
import com.sunday.boot3.user.center.repository.dao.RoleDao;
import com.sunday.boot3.user.center.repository.entity.Role;
import com.sunday.boot3.user.center.service.RoleService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @Description: 角色表 服务实现类
 * @author: zsy
 * @Create: 2024/09/22 16:24:38
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class RoleServiceImpl implements RoleService {

    private final RoleDao roleDao;

    /**
     * @Description: 新增 角色表 数据
     * @Param: [saveRoleReqDTO]
     * @Return: void
     * @Author: zsy
     * @Time: 2024/09/22 16:24:38
     */
    @Override
    public void save(SaveRoleReqDTO saveRoleReqDTO) {
        Role role = MapperConvert.map(saveRoleReqDTO, Role.class);
        role.setId(IdWorker.getId());
        role.setCreateTime(LocalDateTime.now());
        BusinessAssert.isTrue(roleDao.save(role), ErrorCodeEnum.A0_440);
    }

    /**
     * @Description: 删除 角色表 数据
     * @Param: [removeRoleReqDTO]
     * @Return: void
     * @Author: zsy
     * @Time: 2024/09/22 16:24:38
     */
    @Override
    public void remove(RemoveRoleReqDTO removeRoleReqDTO) {
        BusinessAssert.isTrue(roleDao.removeByIds(removeRoleReqDTO.getIds()), ErrorCodeEnum.A0_440);
    }

    /**
     * @Description: 修改 角色表 数据
     * @Param: [updateRoleReqDTO]
     * @Return: void
     * @Author: zsy
     * @Time: 2024/09/22 16:24:38
     */
    @Override
    public void update(UpdateRoleReqDTO updateRoleReqDTO) {
        LambdaUpdateWrapper<Role> roleUW = Wrappers.lambdaUpdate();
        roleUW.eq(Role::getId, updateRoleReqDTO.getId());
        roleUW.set(Role::getName, updateRoleReqDTO.getName());
        BusinessAssert.isTrue(roleDao.update(roleUW), ErrorCodeEnum.A0_440);
    }

    /**
     * @Description: 获取 角色表 分页列表数据
     * @Param: [pageRoleReqDTO]
     * @Return: com.sunday.common.core.remoting.base.page.PageResultDTO<com.sunday.boot3.user.center.pojo.dto.role.PageRoleResDTO>
     * @Author: zsy
     * @Time: 2024/09/22 16:24:38
     */
    @Override
    public PageResultDTO<PageRoleResDTO> page(PageRoleReqDTO pageRoleReqDTO) {
        LambdaQueryWrapper<Role> roleQW = Wrappers.lambdaQuery();
        roleQW.eq(ObjectUtils.isNotEmpty(pageRoleReqDTO.getId()), Role::getId, pageRoleReqDTO.getId())
                .eq(StringUtils.isNotBlank(pageRoleReqDTO.getName()), Role::getName, pageRoleReqDTO.getName())
                .eq(ObjectUtils.isNotEmpty(pageRoleReqDTO.getCreateTime()), Role::getCreateTime, pageRoleReqDTO.getCreateTime());
        return PageWrapper.result(roleDao.page(PageWrapper.map(pageRoleReqDTO, Role::getId), roleQW), data -> MapperConvert.map(data, PageRoleResDTO.class));
    }

}
